package fauxman.states;

import java.awt.Point;
import java.util.ArrayList;

import fauxman.DynamicElements.Ghost;
import fauxman.model.WorldModel;
import fauxman.utilities.ActionUtil;
import fauxman.utilities.SpriteUtil;


public class VulnerableGhostState extends GhostState {
	private static VulnerableGhostState instance;

	
	private VulnerableGhostState(){
	}
	
	public static VulnerableGhostState getInstance(){
		if (instance==null)
			instance = new VulnerableGhostState();
		return instance;
	}
	
	public void setAppearance(int appearanceID, Ghost g) {
		g.setAppearance(SpriteUtil.getVulnerableSprite(appearanceID));
	}
	
	//Vulnerable Ghost wander around aimlessly ( random )
	@Override
	protected int[] coreGhostAI(WorldModel model, Ghost ghost) {
		ArrayList<int[]> choices =  model.validPlayableAreaIntersectionDirections((int)ghost.getLookAhead().getX(),(int)ghost.getLookAhead().getY());
		Point currentP;
		int gX = ghost.getDiscretePosition().x;
		int gY = ghost.getDiscretePosition().y;
		int pX,pY;
		int[] currentDirection;
		while(true)
		{
			int selection =(int)(Math.random()*choices.size());
			currentDirection = choices.get(selection);
			currentP = ActionUtil.getNextPosition(ghost.getLookAhead(), ActionUtil.DISCRETE_UNITS, currentDirection);
			pX = currentP.x;
			pY = currentP.y;
			//Dont go the reverse direction
			if(!(gY == pY && gX == pX))
			{
				ghost.setNextDirection(currentDirection);
				return currentDirection;
			}
			choices.remove(selection);
		}
	}

	//Vulnerable Ghost doesnt have a target since it wanders aimlessly
	public int[] getTarget(WorldModel model, Ghost g) {
		return null;
	}
	
	@Override
	public boolean shouldCalculateNextDirection(WorldModel model, int disX, int disY, int lookaheadX, int lookaheadY) {
		return true;
	}

}
